package general;

import java.awt.Graphics;
import java.awt.print.PageFormat;
import java.awt.print.Printable;
import java.awt.print.PrinterException;
import java.awt.print.PrinterJob;
import java.io.ByteArrayInputStream;
import java.io.InputStream;

import javax.print.Doc;
import javax.print.DocFlavor;
import javax.print.DocPrintJob;
import javax.print.PrintException;
import javax.print.PrintService;
import javax.print.PrintServiceLookup;
import javax.print.SimpleDoc;

public class PrinterDialog implements Printable {
	public static void main(String a []) throws NoPrinterInstalledFoundException, PrintException{
		new PrinterDialog().printText("agfdgfdgfdgfdgdfgdfg\nrdrdrsdrsdrsdrsrsd\t\n\n\n\n\niidsfidsjfoisdjfojdsfsd");
	}

	private String textToBePrint; 

	public void printText(String textToBePrint) throws NoPrinterInstalledFoundException, PrintException{
		this.textToBePrint = textToBePrint;

		/* Checks installed printers. */
		DocFlavor df = DocFlavor.SERVICE_FORMATTED.PRINTABLE;  
		PrintService[] ps = PrintServiceLookup.lookupPrintServices(df, null);  
		if(ps == null){
			throw new NoPrinterInstalledFoundException();
		}

		/* Shows printers dialog. */
		PrinterJob printJob = PrinterJob.getPrinterJob();
		printJob.setPrintable(this);
		if (printJob.printDialog()){
//			try { 
			printJob.setJobName("b");
				/* Calls print method. */
				DocPrintJob dpj = printJob.getPrintService().createPrintJob();  
				InputStream stream = new ByteArrayInputStream(textToBePrint.getBytes());  
				DocFlavor flavor = DocFlavor.INPUT_STREAM.AUTOSENSE;  
				Doc doc = new SimpleDoc(stream, flavor, null);  
				dpj.print(doc, null); 
				//				printJob.print();
				//				System.out.println();
//			} catch(PrinterException pe) {
//				System.out.println("Error printing: " + pe);
//			}
		}
	}

	@Override
	public int print(Graphics graphics, PageFormat pageFormat, int pageIndex)
			throws PrinterException {
		System.out.println();
		System.out.println();
		//passar texto
		//pags 9999
		//colocar no meio a tela
		//nome do arquvio java print
		return 0;
	}

	public void setTextToBePrint(String textToBePrint){
		this.textToBePrint = textToBePrint;
	}

	public String getTextToBePrint(){
		return textToBePrint;
	}

	//	public void b(){
	//		PrinterJob job = PrinterJob.getPrinterJob();
	//		job.setPrintable(this);
	//		boolean ok = job.printDialog();
	//		if (ok)
	//		{
	//			try
	//			{
	//				job.print();
	//			}
	//			catch (PrinterException ex)
	//			{
	//				/* The job did not successfully complete */
	//			}
	//		}
	//
	//
	//	}
	//
	//	public int print(Graphics g, PageFormat pf, int page) throws
	//	PrinterException {
	//
	//		if (page > 0) { /* We have only one page, and 'page' is zero-based */
	//			return NO_SUCH_PAGE;
	//		}
	//
	//		/* User (0,0) is typically outside the imageable area, so we must
	//		 * translate by the X and Y values in the PageFormat to avoid clipping
	//		 */
	//		Graphics2D g2d = (Graphics2D)g;
	//		g2d.translate(pf.getImageableX(), pf.getImageableY());
	//
	//		/* Now print the window and its visible contents */
	//		JFrame frame = new JFrame();
	//		frame.setBounds(100, 100, 697, 89);
	//		frame.setTitle("Aspect Library");
	//		frame.setIconImage(Toolkit.getDefaultToolkit().getImage("C:\\Users\\F. OMENA\\Dropbox\\Mestrado\\Projeto-Mestrado\\AspectLibrary\\Images\\logo\\logo3.png"));
	//		frame.setLocation(
	//				((Toolkit.getDefaultToolkit().getScreenSize().width / 2) - (frame
	//						.getWidth() / 2)), ((Toolkit.getDefaultToolkit()
	//						.getScreenSize().height / 2) - (frame.getHeight() / 2)));
	//		
	//		frame.paint(g2d);
	//		frame.repaint();
	//		/* tell the caller that this page is part of the printed document */
	//		return PAGE_EXISTS;
	//
	//	}
}